#include "Emitter.h"
#include "../SGD Wrappers/SGD_String.h"


Emitter::Emitter(string filepath, int maxParticles)
{
	TOSTRINGSTREAM tss;
	tss << filepath.c_str();

	imageID = CSGD_TextureManager::GetInstance()->LoadTexture( tss.str().c_str() );

	this->maxParticles = maxParticles;

	timesincespawn = 0.0f;
}


//Use to Initialize Particle Array call only after setting all Emitter information
void Emitter::ParticleInitialize( void )
{
	for (int i = 0; i < maxParticles; i++)
	{
		Particle* tempparticle = new Particle();
		tempparticle->SetEmitter( this );
		tempparticle->Initalize();
		ParticleArray.push_back(*tempparticle);
		delete tempparticle;
	}
}

Emitter::~Emitter(void)
{
}

void Emitter::SetShape( string shape )
{
	this->shape = shape;
}

void Emitter::SetActive( bool isActive )
{
	this->isActive = isActive;
}

void Emitter::SetPosX( float PosX )
{
	this->positionX = PosX;
}

void Emitter::SetPosY( float PosY )
{
	this->positionY = PosY;
}

void Emitter::SetSpeedMin( float SpeedMin )
{
	this->initialSpeedMin = SpeedMin;
}

void Emitter::SetSpeedMax( float SpeedMax )
{
	this->initialSpeedMax = SpeedMax;
}

void Emitter::SetLifetimeMin( float lifetimeMin )
{
	this->lifetimeMin = lifetimeMin;
}

void Emitter::SetLifetimeMax( float lifetimeMax )
{
	this->lifetimeMax = lifetimeMax;
}

void Emitter::SetOffsetX( float offsetX )
{
	this->offsetX = offsetX;
}

void Emitter::SetOffsetY( float offsetY )
{
	this->offsetY = offsetY;
}

void Emitter::SetInitialScale( float Scale )
{
	this->initialScale = Scale;
}

void Emitter::SetFinalScale( float Scale )
{
	this->finalScale = Scale;
}

void Emitter::SetInitialRotation( float angleofRotation )
{
	this->initialRotation = angleofRotation;
}

void Emitter::SetFinalRotation( float angleofRotation )
{
	this->finalRotation = angleofRotation;
}

void Emitter::SetAngleRange( float anglerange )
{
	this->angleRange = anglerange;
}

void Emitter::SetEmitterRotation( float angleofrotation )
{
	this->emitterRotation = angleofrotation;
}

void Emitter::SetImageID( int ID )
{
	this->imageID = ID;
}

int Emitter::GetImageID( void )
{
	return imageID;
}

string Emitter::GetShape( void )
{
	return shape;
}

tVector2D Emitter::GetPosition( void )
{
	tVector2D position = { positionX, positionY };
	return position;
}

float Emitter::GetOffsetX( void )
{
	return offsetX;
}

float Emitter::GetOffsetY( void )
{
	return offsetY;
}

float Emitter::GetEmitterRotation( void )
{
	return emitterRotation;
}

float Emitter::GetSpeedMin( void )
{
	return initialSpeedMin;
}

float Emitter::GetSpeedMax( void )
{
	return initialSpeedMax;
}

float Emitter::GetLifetimeMin( void )
{
	return lifetimeMin;
}

float Emitter::GetLifetimeMax( void )
{
	return lifetimeMax;
}


float Emitter::GetInitialScale( void )
{
	return initialScale;
}

float Emitter::GetFinalScale( void )
{
	return finalScale;
}

float Emitter::GetInitialRotation( void )
{
	return initialRotation;
}

float Emitter::GetFinalRotation( void )
{
	return finalRotation;
}

float Emitter::GetAngleRange( void )
{
	return angleRange;
}

void Emitter::Update(float deltaTime)
{
	for (int i = 0; i < maxParticles; i++)
	{
		if(ParticleArray[i].GetActive())
			ParticleArray[i].Update(deltaTime);
	}
	timesincespawn += deltaTime;
	if(timesincespawn >= spawnRate)
	{
		timesincespawn = 0.0f;
		if(!doesLoop && remainingparticles > 0)
		{
			remainingparticles--;
			for (int i = 0; i < maxParticles; i++)
			{
				if (ParticleArray[i].GetActive())
					continue;
				else
				{
					ParticleArray[i].SetActive( true );
					break;
				}
			}
		}
		else if(doesLoop)
		{
			for (int i = 0; i < maxParticles; i++)
			{
				if (ParticleArray[i].GetActive())
					continue;
				else
				{
					ParticleArray[i].SetActive( true );
					break;
				}
			}
		}
	}

}

void Emitter::Render( void ) const
{
	for (int i = 0; i < maxParticles; i++)
	{
		if (ParticleArray[i].GetActive())
		{
			ParticleArray[i].Render();
		}
	}
}